package com.iwhalecloud.bss.kite.cucc.service.util;

import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcSystemParamCache;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <Description> 实名制工具类 <br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @date 2019-09-29 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.common.util <br>
 */
public class RealNameUtil {

    /**
     * 是否特殊客户
     * 16~120岁
     * @param certi_number 证件号码
     * @return
     */
    public static boolean isSpecialGroup(String certi_number) {
        try {
            int leh = certi_number.length();
            String year;
            String month;
            String day;
            if (leh == 18) {
                year = certi_number.substring(6, 10);
                month = certi_number.substring(10, 12);
                day = certi_number.substring(12, 14);
            }
            else if (leh == 15) {
                year = "19" + certi_number.substring(6, 8);
                month = certi_number.substring(8, 10);
                day = certi_number.substring(10, 12);
            }
            else {
                return false;
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
            String curDate = df.format(new Date());
            String curYear = curDate.substring(0, 4);
            String curMonth = curDate.substring(4, 6);
            String curDay = curDate.substring(6);
            int u = Integer.parseInt(curYear) - Integer.parseInt(year);
            int m = Integer.parseInt(curMonth) - Integer.parseInt(month);
            int d = Integer.parseInt(curDay) - Integer.parseInt(day);
            int beginAge = Integer.parseInt(getMinAge());
            int endAge = Integer.parseInt(getMaxAge());
            if (u <= beginAge || u >= endAge) {
                if (u == beginAge) {
                    // 未成年人临界值判断
                    if (m > 0) {
                        return false;
                    }
                    else if (m == 0) {
                        if (d >= 0) {
                            return false;
                        }
                    }
                }
                if (u == endAge) {
                    // 老年人临界值判断
                    if (m < 0) {
                        return false;
                    }
                    else if (m == 0) {
                        if (d <= 0) {
                            return false;
                        }
                    }
                }
                return true;
            }
            return false;
        }
        catch (Exception e) {
            return true;
        }
    }

    /**
     * 获取允许办理业务的最小年龄
     *
     * @return
     */
    public static String getMinAge() {
        return DcSystemParamCache.getDcParamVal(LocalKeyConsts.CUST_AGENUM_MIN);
    }

    /**
     * 获取允许办理业务的最大年龄
     *
     * @return
     */
    public static String getMaxAge() {
        return DcSystemParamCache.getDcParamVal(LocalKeyConsts.CUST_AGENUM_MAX);
    }

}
